package vn.vfriends.vcms.site.service.impl;

import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import vn.vfriends.vcms.site.model.Category;
import vn.vfriends.vcms.site.model.Item;
import vn.vfriends.vcms.site.model.Status;
import vn.vfriends.vcms.site.service.ItemService;

/**
 *
 * @author tuan@vfriends.vn
 */
public class ItemJpaController implements ItemService {

    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "vcmsPU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<Item> findItems() {
        return findItems(true, -1, -1);
    }

    @Override
    public List<Item> findItems(int maxResults, int firstResult) {
        return findItems(false, maxResults, firstResult);
    }

    private List<Item> findItems(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Item.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    
    @Override
    public List<Item> findItems(Category category, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery cq = cb.createQuery();
            Root<Item> rt = cq.from(Item.class);
            cq.select(rt);
            
            Predicate predicate = cb.conjunction();
            if(category != null) {
                predicate = cb.and(predicate, cb.equal(rt.get("category"), category));
            } else {
                predicate = cb.and(predicate, cb.isNull(rt.get("category")));
            }
            predicate = cb.and(predicate, cb.equal(rt.get("status"), Status.PUBLISHED));
            
            cq.where(predicate);
            
            Query q = em.createQuery(cq);
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    @Override
    public Item findItem(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Item.class, id);
        } finally {
            em.close();
        }
    }

    @Override
    public int getItemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Item> rt = cq.from(Item.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    @Override
    public int getItemCount(Category category) {
        EntityManager em = getEntityManager();
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery cq = cb.createQuery();
            Root<Item> rt = cq.from(Item.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            
            Predicate predicate = cb.conjunction();
            predicate = cb.and(predicate, cb.equal(rt.get("category"), category));
            predicate = cb.and(predicate, cb.equal(rt.get("status"), Status.PUBLISHED));
            
            cq.where(predicate);
            
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
